package com.library.controller;


import com.library.common.Result;
import com.library.entity.Books;
import com.library.service.IBooksService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LDH
 * @since 2022-05-12
 */
@RestController
@RequestMapping("/library/books")
public class BooksController {

    @Autowired
    private IBooksService booksService;


    @ApiOperation(value = "获取所有图书信息",notes = "<span style='color:red;'>详细描述：</span>&nbsp;获取所有图书信息")
    @GetMapping
    public Result getBookAll(){
        return booksService.getBookAll();
    }

    @ApiOperation(value = "添加单本书籍",notes = "<span style='color:red;'>详细描述：</span>&nbsp;传入书籍类实体，返回新添加单本书籍信息")
    @PostMapping("/book")
    public Result addBook(@Valid @RequestBody Books books){
        return booksService.addBook(books);
    }

    @ApiOperation(value = "删除一本书",notes = "<span style='color:red;'>详细描述：</span>&nbsp;传入id删除对应的书籍")
    @DeleteMapping("/{id}")
    public Result deleteBook(@PathVariable int id){
        return booksService.deleteBook(id);
    }

    @ApiOperation(value = "编辑书籍",notes = "<span style='color:red;'>详细描述：</span>&nbsp;传入单本书籍实体信息，返回更新后的书籍信息")
    @PutMapping
    public Result putBook(@Valid @RequestBody Books books){
        return booksService.putBook(books);
    }

    @ApiOperation(value = "模糊查询",notes = "<span style='color:red;'>详细描述：</span>&nbsp;根据传来的字符，模糊查找类似的书名和作者名，并返回一个对象集")
    @GetMapping("/like")
    public Result likeBook(@RequestParam String name){
        return booksService.likeBook(name);
    }

    @ApiOperation(value = "根据id组批量删除书籍",notes = "<span style='color:red;'>详细描述：</span>&nbsp;传来一组书籍id，key值为“books”，然后批量删除")
    @DeleteMapping("/books")
    public Result deleteByList(@Valid @RequestBody Map<String,List> ids){
        return booksService.deleteByList(ids);
    }

    @ApiOperation(value = "批量编辑书籍",notes = "<span style='color:red;'>详细描述：</span>&nbsp;传来一组书籍对象组，批量编辑完成后返回这批书籍的数据组")
    @PutMapping("/books")
    public Result putByList(@Valid @RequestBody Map<String,List<Books>> bookMap){
        return booksService.putByList(bookMap);
    }

    @ApiOperation(value = "分页查询",notes = "<span style='color:red;'>详细描述：</span>&nbsp;传来要查的页数和每页的大小，返回那一页的书籍数据")
    @GetMapping("/page")
    public Result bookPage(@RequestParam int page, @RequestParam int pageSize){
        return booksService.booksPage(page,pageSize);
    }
}
